统一异常断言为 Assert.Throws/ThrowsAsync 并升级 MSTest#53
Conversation
将所有单元测试中的异常断言方式统一为 .NET 6+ 的 Assert.Throws/Assert.ThrowsAsync,移除 [ExpectedException] 特性,替换原有 Assert.ThrowsException/ThrowsExceptionAsync。同步升级 MSTest 相关 NuGet 包版本以支持新 API。此举提升了测试代码的现代性和可维护性,并兼容新版 MSTest 框架。
There was a problem hiding this comment.
Pull request overview
This pull request modernizes the test suite by standardizing exception assertions and upgrading MSTest packages. The changes replace outdated [ExpectedException] attributes and Assert.ThrowsException/ThrowsExceptionAsync methods with the modern .NET 6+ compatible Assert.Throws/ThrowsAsync pattern across all unit tests.
Changes:
- Replaced all
Assert.ThrowsExceptionwithAssert.ThrowsandAssert.ThrowsExceptionAsyncwithAssert.ThrowsAsync - Removed
[ExpectedException]attributes and wrapped test code inAssert.Throwslambdas - Updated MSTest package versions to support the new assertion APIs
Reviewed changes
Copilot reviewed 35 out of 35 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| LuYao.Text.Json.UnitTests.csproj | Upgraded Microsoft.NET.Test.Sdk to 18.0.1 and MSTest to 4.0.2 |
| LuYao.Common.UnitTests.csproj | Upgraded MSTest packages to 4.0.2 and Microsoft.NET.Test.Sdk to 18.0.1 |
| TranslatableJsonModelTests.cs | Updated exception assertions to Assert.Throws |
| TranslatableXmlModelTests.cs | Removed [ExpectedException] and added Assert.Throws wrapper |
| TaskExtensionsTests.cs | Changed async exception assertion to Assert.ThrowsAsync |
| Multiple test files | Standardized exception assertion patterns throughout the test suite |
| KeyedLockerTests.cs, PathHelperTests.cs, etc. | Chinese comment encoding appears corrupted |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| /// <summary> | ||
| /// 测试 KeyedLocker<T> 类型的功能。 | ||
| /// ���� KeyedLocker<T> ���͵Ĺ��ܡ� |
There was a problem hiding this comment.
The Chinese comments appear to be corrupted with encoding issues. The text shows garbled characters instead of proper Chinese text. This affects readability and documentation quality. Please verify the file encoding is UTF-8 and restore the original Chinese characters.
| { | ||
| /// <summary> | ||
| /// 测试使用有效键获取锁对象时,锁对象应正确返回。 | ||
| /// ����ʹ����Ч����ȡ������ʱ��������Ӧ��ȷ���ء� |
There was a problem hiding this comment.
The Chinese comments appear to be corrupted with encoding issues. The text shows garbled characters instead of proper Chinese text. This affects readability and documentation quality. Please verify the file encoding is UTF-8 and restore the original Chinese characters.
|
|
||
| // Assert | ||
| Assert.IsNotNull(lockObject, "锁对象不应为 null。"); | ||
| Assert.IsNotNull(lockObject, "������ӦΪ null��"); |
There was a problem hiding this comment.
The Chinese text in the assertion message appears to be corrupted with encoding issues. The text shows garbled characters instead of proper Chinese text. This affects test output readability. Please verify the file encoding is UTF-8 and restore the original Chinese characters.
|
|
||
| /// <summary> | ||
| /// 测试 PathHelper 类的功能。 | ||
| /// ���� PathHelper ��Ĺ��ܡ� |
There was a problem hiding this comment.
The Chinese comments appear to be corrupted with encoding issues. The text shows garbled characters instead of proper Chinese text. This affects readability and documentation quality. Please verify the file encoding is UTF-8 and restore the original Chinese characters.
| /// ���� PathHelper ��Ĺ��ܡ� | |
| /// 测试 PathHelper 类的功能。 |
| { | ||
| // Arrange | ||
| var content = new StringContent("测试内容", Encoding.Unicode); | ||
| var content = new StringContent("��������", Encoding.Unicode); |
There was a problem hiding this comment.
The Chinese text literal appears to be corrupted with encoding issues. The text shows garbled characters instead of proper Chinese text. This will cause test failures as the expected content won't match. Please verify the file encoding is UTF-8 and restore the original Chinese characters.
|
|
||
| // Assert: 期望抛出 ArgumentOutOfRangeException | ||
| // Arrange & Act & Assert | ||
| Assert.Throws<ArgumentOutOfRangeException>(() => { var item = _keyedList[-1]; }); |
There was a problem hiding this comment.
This assignment to item is useless, since its value is never read.
| Assert.Throws<ArgumentOutOfRangeException>(() => { var item = _keyedList[-1]; }); | |
| Assert.Throws<ArgumentOutOfRangeException>(() => _keyedList[-1]); |
将所有单元测试中的异常断言方式统一为 .NET 6+ 的 Assert.Throws/Assert.ThrowsAsync,移除 [ExpectedException] 特性,替换原有 Assert.ThrowsException/ThrowsExceptionAsync。同步升级 MSTest 相关 NuGet 包版本以支持新 API。此举提升了测试代码的现代性和可维护性,并兼容新版 MSTest 框架。